package cn.semes.org.controller;

import cn.semes.basic.util.ExcelUtil;
import cn.semes.org.dto.AuditShopDto;
import cn.semes.org.dto.ShopSettlementDto;
import cn.semes.org.service.IShopService;
import cn.semes.org.domain.Shop;
import cn.semes.org.query.ShopQuery;
import cn.semes.basic.util.AjaxResult;
import cn.semes.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 店铺入住
     * @param dto
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopSettlementDto dto){
        shopService.settlement(dto);
        return AjaxResult.me().success();
    }

    // 审核通过
    @PostMapping("/audit/pass")
    public AjaxResult settlement(@RequestBody @Valid AuditShopDto dto) throws MessagingException {
        shopService.AuditShopPass(dto);
        return AjaxResult.me().success();
    }
    // 审核驳回
    @PostMapping("/audit/reject")
    public AjaxResult settlemen(@RequestBody @Valid AuditShopDto dto) throws Exception {
        shopService.AuditShopReject(dto);
        return AjaxResult.me().success();
    }

    // 激活店铺
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id)
    {
        shopService.active(id);
        return AjaxResult.me().success();
    }

    // 驳回重新填写
    @PostMapping("/settlement/again")
    public AjaxResult reset(@RequestBody @Valid Shop dto)
    {
        shopService.resetSettlement(dto);
        return AjaxResult.me().success();
    }

    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,null,"店铺信息",Shop.class,"店铺信息.xls",response);
    }

    @GetMapping("/imoprt/excel")
    public void exportExcel(@RequestPart("file")MultipartFile file){
        List<Shop> all = ExcelUtil.importExcel(file,0,1,Shop.class);
       // shopService.adds(all);
    }
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryByPage(query);
    }
}
